package com.olinsports.system.olinsportssystem.service;

import com.olinsports.system.olinsportssystem.model.*;
import com.olinsports.system.olinsportssystem.repository.*;
import com.olinsports.system.olinsportssystem.util.CommodityConsumptionExcel;
import com.olinsports.system.olinsportssystem.util.PageUtil;
import com.olinsports.system.olinsportssystem.util.SiteConsumptionDetailsExcel;
import com.olinsports.system.olinsportssystem.util.ViewExcel;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Date;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/22.
 */
@Service
public class CommodityConsumptionService {
    @Autowired
    private CommodityConsumptionRepository commodityConsumptionRepository;
    @Autowired
    private StmUsrRepository stmUsrRepository;
    @Autowired
    private CommodityTypeRepository commodityTypeRepository;
    @Autowired
    private CommodityDetailedRepository commodityDetailedRepository;
    private int totalPages = 0;//总页数

    private int  totalRows;//总数据量
    @Autowired
    private PageUtil pageUtil;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private HttpServletResponse response;


    public CommodityConsumption selectByKey(int conKey){
        return commodityConsumptionRepository.selectByKey(conKey);
    }

    public Map listCommConsum(String page, String rows,String sord, String sidx){
        int beg = (Integer.valueOf(page)-1)*Integer.valueOf(rows);
        //查询当前表中的总数据量
        totalRows = commodityConsumptionRepository.findCount();
        System.out.println("totalRows = "+totalRows);
        //通过分页进行查询当前表中的数据
        List<CommodityConsumption> commodityConsumptions =  commodityConsumptionRepository.findLimit(sord,sidx,beg,Integer.valueOf(rows));

        Map mapList = pageUtil.paging(rows,page,totalRows);
        mapList.put("rows",commodityConsumptions);//设置行显示的数据

        return mapList;
    }

    public Map searchDate(String page, String rows,String startDate,String endDate){
        int beg = (Integer.valueOf(page)-1)*Integer.valueOf(rows);
        totalRows = commodityConsumptionRepository.selectByDateCount(startDate,endDate);
        List<CommodityConsumption> commodityConsumptions =  commodityConsumptionRepository.selectByDateLimit(startDate,endDate,beg,Integer.valueOf(rows));
        Map mapList = pageUtil.paging(rows,page,totalRows);
        mapList.put("rows",commodityConsumptions);//设置行显示的数据
        return mapList;
    }


    public ModelAndView toDcExcel(String startDate, String endDate, HSSFWorkbook workbook) throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        List<CommodityConsumption> commodityConsumptions = commodityConsumptionRepository.selectByDateAll(startDate,endDate);
        map.put("inventoryDetailss",commodityConsumptions);
        CommodityConsumptionExcel commodityConsumptionExcel = new CommodityConsumptionExcel("商品消费明细","商品消费明细");
        commodityConsumptionExcel.buildExcelDocument(map, workbook, request, response);
        return new ModelAndView(commodityConsumptionExcel, map);
    }

    public Map searchSelf(String page, String rows,String field,String fieldValue){
        int beg = (Integer.valueOf(page)-1)*Integer.valueOf(rows);
        totalRows = commodityConsumptionRepository.selectSelfCount(field,fieldValue);
        List<CommodityConsumption> commodityConsumptions =  commodityConsumptionRepository.selectSelfLimit(field,fieldValue,beg,Integer.valueOf(rows));
        Map mapList = pageUtil.paging(rows,page,totalRows);
        mapList.put("rows",commodityConsumptions);//设置行显示的数据
        return mapList;
    }

    public int insertAll(String commDetailed){
        JSONObject jsonObject = new JSONObject(commDetailed);
        String vipType = jsonObject.getString("VipType");
        String skuName = jsonObject.getString("SkuName");
        String detailedKey = jsonObject.getString("detailedKey");
        String price = jsonObject.getString("price");
        String quantity = jsonObject.getString("quantity");
        int sum = jsonObject.getInt("sum");
        int usrKey = jsonObject.getInt("usrKey");
        String paymentMethod = jsonObject.getString("paymentMethod");
        String vipNum = jsonObject.getString("vipNum");

        Date date = new Date(System.currentTimeMillis());//获取当前时间
        Format format = new SimpleDateFormat("yyyyMMdd-HHmmss");//时间格式化
        StmUsr stmUsr = (StmUsr)request.getSession().getAttribute("stmUsr");

        String skuType = commodityTypeRepository.selectByDetailedKey(Integer.valueOf(detailedKey));
        StmUsr operatorUsr = stmUsrRepository.selectByUsrKey(usrKey);

        CommodityDetailed commodityDetailed = commodityDetailedRepository.selectByKey(Integer.valueOf(detailedKey));
        int stock = commodityDetailed.getStock() - Integer.valueOf(quantity);

        CommodityConsumption commodityConsumption = new CommodityConsumption();
        commodityConsumption.setVipType(vipType);
        commodityConsumption.setSkuName(skuName);
        commodityConsumption.setPrice(Integer.valueOf(price));
        commodityConsumption.setQuantity(Integer.valueOf(quantity));
        commodityConsumption.setSum(sum);
        commodityConsumption.setPaymentMethod(paymentMethod);
        commodityConsumption.setVipNum(vipNum);
        commodityConsumption.setSkuType(skuType);
        commodityConsumption.setDate(date);
        commodityConsumption.setOperatorNum(operatorUsr);
        commodityConsumption.setComConOrder(format.format(date));
        if(stock < 0){
            return -1;
        } else {
            commodityDetailed.setStock(stock);
            int con = commodityConsumptionRepository.insertAll(commodityConsumption);
            int comm = commodityDetailedRepository.updateData(commodityDetailed);
            if(con > 0 && comm > 0){
                return 1;
            } else {
                return -1;
            }
        }

    }

    public int getSumTotal(){
        List<CommodityConsumption> commodityConsumptions = commodityConsumptionRepository.selectAll();
        int sumTotal = 0;
        for(CommodityConsumption commodityConsumption : commodityConsumptions){
            sumTotal += commodityConsumption.getSum();
        }
        return sumTotal;
    }

}
